package com.tledu.zyf.model;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import com.tledu.zyf.util.Base64Util;
import com.tledu.zyf.util.IPUtil;

/**
 * 结构化实体类
 * 
 * @author yingfing
 * @Date 2020年10月30日 @time 下午6:23:39
 */
public class IPAndLocationPojo implements Comparable<IPAndLocationPojo>,
		Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 起始IP(前字段)
	 */
	private transient String startIP;
	/**
	 * 结束IP(后字段)
	 */
	private transient String endIP;
	/**
	 * 归属地
	 */
	private String location;

	private long startIPLong;
	private long endIPLong;

	public String getStartIP() {
		return startIP;
	}

	public void setStartIP(String startIP) {
		this.startIP = startIP;
	}

	public long getStartIPLong() {
		return startIPLong;
	}

	public void setStartIPLong(long startIPLong) {
		this.startIPLong = startIPLong;
	}

	public long getEndIPLong() {
		return endIPLong;
	}

	public void setEndIPLong(long endIPLong) {
		this.endIPLong = endIPLong;
	}

	public String getEndIP() {
		return endIP;
	}

	public void setEndIP(String endIP) {
		this.endIP = endIP;
	}

	public String getLocation() {
		// try {
		// return Base64Util.decode(location);
		// } catch (UnsupportedEncodingException e) {
		// e.printStackTrace();
		// }
		return location;
	}

	public void setLocation(String location) {
		try {
			this.location = Base64Util.encode(location);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public IPAndLocationPojo(String startIP, String endIP, String location)
			throws UnsupportedEncodingException {
		super();
		this.startIP = startIP;
		this.endIP = endIP;
		// this.location = Base64Util.encode(location);
		this.location = location;
		// 根据IP生成长整型
		this.startIPLong = IPUtil.ipToLong(this.startIP);
		this.endIPLong = IPUtil.ipToLong(this.endIP);
	}

	public IPAndLocationPojo() {
		super();
	}

	@Override
	public String toString() {
		return "IPAndLocationPojo [startIP=" + startIP + ", endIP=" + endIP
				+ ", location=" + location + ", startIPLong=" + startIPLong
				+ ", endIPLong=" + endIPLong + "]";
	}

	@Override
	public int compareTo(IPAndLocationPojo o) {
		long status = this.startIPLong - o.startIPLong;
		return status > 0 ? 1 : (status < 0 ? -1 : 0);
	}

}
